using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Bll;
using Entities;

public partial class billing : MyPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IsMemberPage = true;
        IsDealerPage = true;
        if (!IsPostBack)
        {
            //lblShipPrice.Text = "";            
            if (Session["ShoppingCart"] != null)
            {
                decimal prize = (decimal)Session["Subtotal"];
                lblSubtotal.Text = prize.ToString("C");
            }
            else
                Response.Redirect("shoppingcart.aspx");
        }
        
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtStreetAddress.Text = "";
        
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            if (!string.IsNullOrEmpty(lblShipPrice.Text))
            {
                CartBll cart = (CartBll)Session["ShoppingCart"];
                decimal prize = (decimal) Session["Subtotal"];
                LineItemInfo[] lines = cart.GetOrderLineItems();

                OrderBll orderBll = new OrderBll();
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.UserName = username;
                //Get back ShipID
                ShipBll shipBll = new ShipBll();
                ShipInfo shipInfo = new ShipInfo();
                string ship_Price = radioShip.SelectedValue;
                string ship_Name = radioShip.SelectedItem.Text.Trim(); 
                shipInfo = shipBll.GetByName(ship_Name);
                //decimal shipPrice = decimal.Parse(ship_Price);
                decimal shipPrice = shipInfo.ShipPrice;
                orderInfo.ShipID = shipInfo.ShipID;
                orderInfo.ShipName = txtFirstName.Text + " " + txtLastName.Text;
                orderInfo.ShipAddress = txtStreetAddress.Text.Trim();
                orderInfo.ShipCity = ddlstCity.SelectedValue.Trim();
                orderInfo.SubTotal = (decimal)prize + shipPrice;

                ArrayList arr = new ArrayList();
                arr.Add(orderInfo);
                arr.Add(shipInfo);                
                Session["Order"] = arr;

                int newOrderID = orderBll.Insert(orderInfo);
                                
                OrderDetailBll orderdetailBll = new OrderDetailBll();
                ProductBll proBll = new ProductBll();
                
                foreach(LineItemInfo line in lines)
                {
                    OrderDetailInfo orderdetailInfo = new OrderDetailInfo();
                    orderdetailInfo.OrderID = newOrderID;
                    orderdetailInfo.ProductID = int.Parse(line.ItemId.Trim());
                    orderdetailInfo.UnitPrice = line.Price;
                    orderdetailInfo.Quantity = line.Quantity;
                    //orderdetailInfo.ShipFee = int.Parse(radioShip.SelectedValue);
                    orderdetailInfo.Total = prize;
                    orderdetailBll.Insert(orderdetailInfo);
                    proBll.UpdateUnitOnOrder(line.Quantity.ToString(), line.ItemId.Trim());
                    
                }
                Response.Redirect("checkoutcomplete.aspx?OrderID=" + newOrderID);               
            }
            else
            {
                lblShipPrice.Text = "Please select Shipping method";
                lblSubtotal.Text = "";
            }
        }
    }
    protected void radioShip_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblShipPrice.Text = "$ " + radioShip.SelectedValue;
        //CartBll cart = (CartBll)Session["ShoppingCart"];
        //decimal subtotal = cart.Total + decimal.Parse(radioShip.SelectedValue);
        decimal prize = (decimal)Session["Subtotal"];
        decimal subtotal = (decimal)prize + decimal.Parse(radioShip.SelectedValue);
        lblSubtotal.Text = prize.ToString("C") + " + "  + lblShipPrice.Text + " = " + "<b>" + subtotal.ToString("C") + "</b>";
    }
}
